<script setup lang="jsx">
import { reactive, ref } from 'vue';
import { UUID } from 'uuidjs';
import { parseTime } from '@/utils';
import { erpsyncmesequMachineInfo, machinelist } from '@/service/api/modules/erp';
import Modal from '@/components/common/modal.vue';
import Form from '@/components/common/form.vue';
import Search from './modules/search.vue';
const searchParams = ref({
  machineNo: '',
  beginTime: '',
  endTime: '',
  deptCaption: '',
  machineName: ''
});

const pagination = reactive({
  current: 1,
  pageSize: 200,
  showSizeChanger: true,
  pageSizeOptions: ['50', '100', '180', '300', '500'],
  showQuickJumper: true,
  showTotal: total => {
    return `共${total}条`;
  },
  total: 0,
  onChange: async (current, size) => {
    pagination.current = current;
    pagination.pageSize = size;
    getData();
  }
});

const columns = ref([
  {
    key: 'index',
    dataIndex: 'index',
    title: '序号',
    width: 100,
    align: 'center',
    checked: true
  },
  {
    key: 'machineName',
    dataIndex: 'machineName',
    title: '设备名称',
    width: 180,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'machineNo',
    dataIndex: 'machineNo',
    title: '设备编码',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'machineType',
    dataIndex: 'machineType',
    title: '设备类型',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'deptCaption',
    dataIndex: 'deptCaption',
    title: '使用部门',
    width: 100,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'modelNo',
    dataIndex: 'modelNo',
    title: '设备型号',
    width: 150,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'producer',
    dataIndex: 'producer',
    title: '制造商',
    width: 180,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'createDate',
    dataIndex: 'createDate',
    title: '创建时间',
    width: 120,
    align: 'center',
    checked: true,
    ellipsis: true,
    customRender: ({ record }) => {
      if (record.createDate == null) {
        return null;
      }
      return parseTime(record.createDate);
    }
  },
  {
    key: 'updateDate',
    dataIndex: 'updateDate',
    title: '修改时间',
    width: 120,
    align: 'center',
    checked: true,
    ellipsis: true,
    customRender: ({ record }) => {
      if (record.updateDate == null) {
        return null;
      }
      return parseTime(record.updateDate);
    }
  },
  {
    key: 'lmTime',
    dataIndex: 'lmTime',
    title: '同步时间',
    width: 120,
    align: 'center',
    checked: true,
    ellipsis: true,
    customRender: ({ record }) => {
      if (record.lmTime == null) {
        return null;
      }
      return parseTime(record.lmTime);
    }
  }
]);
const tableData = ref([]);
const loading = ref(false);
const defaultExpandedRowKeys = ref([]);

function getData() {
  loading.value = true;
  machinelist({
    pageSize: pagination.pageSize,
    pageNum: pagination.current,
    ...searchParams.value
  })
    .then(({ data, error }) => {
      if (!error) {
        data.list.forEach((item, index) => {
          item.index = index + 1;
          item.uid = UUID.genV4().hexNoDelim;
        });
        tableData.value = data.list;
        pagination.total = data.total;
      }
    })
    .finally(() => {
      loading.value = false;
    });
}
const crDatas = ref({});
const detailsVisibleQrcode = ref(false);
const formListqr = [
  {
    type: 'input',
    key: 'machineNo',
    label: '设备编码',
    placeholder: '请输入设备编码',
    span: 24
  }
];

function manualSyncing() {
  crDatas.value = {};
  detailsVisibleQrcode.value = true;
}
function boundUpdata() {
  erpsyncmesequMachineInfo(crDatas.value).then(({ data, error }) => {
    if (!error) {
      window.$message?.success('手动同步成功');
      detailsVisibleQrcode.value = false;
      getData();
    }
  });
}
getData();

function search() {
  pagination.current = 1;
  getData();
}

const currentRowId = ref(null);
const customRow = record => {
  return {
    style: {
      background: record.uid === currentRowId.value ? '#BAEAFA' : ''
    },
    class: record.uid === currentRowId.value ? 'jl-table-row-selected' : '',
    onClick: () => {
      currentRowId.value = record.uid;
    }
  };
};
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <Search v-model:model="searchParams" @reset="search()" @search="search()" />
    <ACard
      :bordered="false"
      :body-style="{ flex: 1, overflow: 'hidden' }"
      class="table-card flex-col-stretch sm:flex-1-hidden card-wrapper"
    >
      <template #title>
        <AButton type="primary" size="small" ghost class="mini-btn mr-8px" @click="manualSyncing">
          <span class="ml-3px">手动同步</span>
        </AButton>
      </template>
      <template #extra>
        <TableColumnSetting v-model:columns="columns" />
      </template>
      <STable
        :columns="columns"
        :data-source="tableData"
        size="small"
        bordered
        :loading="loading"
        :default-expanded-row-keys="defaultExpandedRowKeys"
        row-key="uid"
        :default-expand-all-rows="true"
        :scroll="{ x: 'auto', y: 'auto' }"
        :pagination="pagination"
        class="h-full"
        :custom-row="customRow"
        :row-height="23"
      />
      <Modal v-model:visible="detailsVisibleQrcode" :destroy-on-close="true" title="手动设备档案" width="550px">
        <div class="event-form">
          <Form v-model:model="crDatas" :form-list="formListqr"></Form>
        </div>

        <template #footer>
          <div class="modal-footer">
            <AButton @click="detailsVisibleQrcode = false">关闭</AButton>
            <AButton type="primary" @click="boundUpdata">保存</AButton>
          </div>
        </template>
      </Modal>
    </ACard>
  </div>
</template>

<style></style>
